<?php

/**
 * profile actions.
 *
 * @package    quotes
 * @subpackage profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class profileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward404Unless($user_id = $this->getUser()->getAttribute('user_id', null,'sfGuardSecurityUser'));


  }

  public function executeShowFavorites(sfWebRequest $request)
  {
    $this->forward404Unless($user_id = $this->getUser()->getAttribute('user_id', null,'sfGuardSecurityUser'));

    $this->pager = QuoteQuery::create()
      ->joinWithAuthor()
      ->useQuoteHelperQuery()
        ->filterByUserId($user_id)
      ->endUse()
      ->paginate($request->getParameter('page', 1), 5);

  }

  public function executeAddFavorite(sfWebRequest $request)
  {
    $this->forward404Unless($quote_id = $request->getParameter('quote_id'));
    $fav = new QuoteHelper();
    $fav->setQuoteId($quote_id);
    $fav->setUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'));
    $fav->save();

    $this->getUser()->setFlash('notice', 'Favorite added!');
    $this->redirect($request->getReferer());
  }

  public function executeRemoveFavorite(sfWebRequest $request)
  {
    $this->forward404Unless($quote_id = $request->getParameter('quote_id'));

    $fav = QuoteHelperQuery::create()
      ->filterByQuoteId($quote_id)
      ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->findOne();

    $this->forward404Unless($fav);
    $this->getUser()->setFlash('notice', 'Favorite removed!');
    $fav->delete();
    $this->redirect($request->getReferer());
  }

  public function executeAddBookmark(sfWebRequest $request)
  {
    $this->forward404Unless($quote_id = $request->getParameter('quote_id'));
    $fav = new QuoteHelper();
    $fav->setQuoteId($quote_id);
    $fav->setUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'));
    $fav->save();

    $this->getUser()->setFlash('notice', 'Bookmark added!');
    $this->redirect($request->getReferer());
  }

  public function executeRemoveBookmark(sfWebRequest $request)
  {
    $this->forward404Unless($quote_id = $request->getParameter('quote_id'));

    $fav = QuoteHelperQuery::create()
      ->filterByQuoteId($quote_id)
      ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->findOne();

    $this->forward404Unless($fav);
    $this->getUser()->setFlash('notice', 'Bookmark removed!');
    $fav->delete();
    $this->redirect($request->getReferer());
  }

  public function executeShowBookmarks(sfWebRequest $request)
  {
    $this->forward404Unless($user_id = $this->getUser()->getAttribute('user_id', null,'sfGuardSecurityUser'));

    $this->pager = QuoteQuery::create()
      ->joinWithAuthor()
      ->useQuoteHelperQuery()
        ->filterByUserId($user_id)
      ->endUse()
      ->paginate($request->getParameter('page', 1), 5);
  }

  public function executeAddFavoriteEssay(sfWebRequest $request)
  {
    $this->forward404Unless($essay_id = $request->getParameter('essay_id'));
    $fav = new EssayHelper();
    $fav->setEssayId($essay_id);
    $fav->setUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'));
    $fav->save();

    $this->getUser()->setFlash('notice', 'Essay added to favorites!');
    $this->redirect($request->getReferer());
  }

  public function executeRemoveFavoriteEssay(sfWebRequest $request)
  {
    $this->forward404Unless($essay_id = $request->getParameter('essay_id'));

    $fav = QuoteHelperQuery::create()
      ->filterByQuoteId($essay_id)
      ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->findOne();

    $this->forward404Unless($fav);
    $this->getUser()->setFlash('notice', 'Essay removed from favorites!');
    $fav->delete();
    $this->redirect($request->getReferer());
  }

  public function executeAddBookmarkEssay(sfWebRequest $request)
  {
    $this->forward404Unless($essay_id = $request->getParameter('essay_id'));
    $fav = new EssayHelper();
    $fav->setEssayId($essay_id);
    $fav->setUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'));
    $fav->setType(EssayHelper::BOOKMARK);
    $fav->save();

    $this->getUser()->setFlash('notice', 'Essay added to bookmarks!');
    $this->redirect($request->getReferer());
  }

  public function executeRemoveBookmarkEssay(sfWebRequest $request)
  {
    $this->forward404Unless($essay_id = $request->getParameter('essay_id'));

    $fav = QuoteHelperQuery::create()
      ->filterByQuoteId($essay_id)
      ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->findOne();

    $this->forward404Unless($fav);
    $this->getUser()->setFlash('notice', 'Essay removed from bookmarks!');
    $fav->delete();
    $this->redirect($request->getReferer());
  }

  public function executeShowFavoriteEssays(sfWebRequest $request)
  {
    $this->essays = EssayQuery::create()
      ->useEssayHelperQuery()
        ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->endUse()
      ->paginate($request->getParameter('page',1), 5);
  }

  public function executeShowBookmarkedEssays(sfWebRequest $request)
  {

  }

    public function executeAddAuthorBookmark(sfWebRequest $request)
  {
    $this->forward404Unless($author_id = $request->getParameter('author_id'));
    $fav = new AuthorHelper();
    $fav->setAuthorId($author_id);
    $fav->setUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'));
    $fav->save();

    $this->getUser()->setFlash('notice', 'Author bookmarked!');
    $this->redirect($request->getReferer());
  }

  public function executeRemoveAuthorBookmark(sfWebRequest $request)
  {
    $this->forward404Unless($author_id = $request->getParameter('author_id'));

    $fav = AuthorHelperQuery::create()
      ->filterByAuthorId($author_id)
      ->filterByUserId($this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser'))
      ->findOne();

    $this->forward404Unless($fav);
    $this->getUser()->setFlash('notice', 'Author unbookmarked!');
    $fav->delete();
    $this->redirect($request->getReferer());
  }

  public function executeShowAuthorBookmarks(sfWebRequest $request)
  {
    $this->forward404Unless($user_id = $this->getUser()->getAttribute('user_id', null,'sfGuardSecurityUser'));

    $this->pager = AuthorQuery::create()
      ->useAuthorHelperQuery()
        ->filterByUserId($user_id)
      ->endUse()
      ->paginate($request->getParameter('page', 1), 5);
  }
}
